package com.huanzhidadi.singleweb.pageinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huanzhidadi.singleweb.pageinfo.mapper.ItemMapper;
import com.huanzhidadi.singleweb.pageinfo.model.Item;
import com.huanzhidadi.singleweb.pageinfo.model.ItemInVO;
import com.huanzhidadi.singleweb.pageinfo.model.ItemOutVO;
import com.huanzhidadi.singleweb.pageinfo.service.ItemService;
import com.huanzhidadi.singleweb.pageinfo.util.PageInfoUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageInfo<ItemOutVO> getItemPageInfo(ItemInVO itemInVO) {
        if (Objects.nonNull(itemInVO.getPageNum()) && Objects.nonNull(itemInVO.getPageSize())) {
            PageHelper.startPage(itemInVO.getPageNum(), itemInVO.getPageSize());
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.isNotBlank(itemInVO.getItemName()), "item_name", itemInVO.getItemName());
        queryWrapper.eq(Objects.nonNull(itemInVO.getItemType()) && itemInVO.getItemType()>0, "item_type", itemInVO.getItemType());
        List list = itemMapper.selectList(queryWrapper);
        PageInfo itemPageInfo = new PageInfo(list);

        return PageInfoUtil.pageInfo2Entity(itemPageInfo, ItemOutVO.class);
    }
}
